package com.ankiang.mykeys;


import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Random;

/***
 * ADAPTER
 */

public class ShortcutAdapter extends ArrayAdapter<JSONObject> {

    private static final String TAG = "ShortcutAdapter";

    static class ViewHolder {
        public TextView txtShortcut;
        public TextView txtShortcutDes;
        public TextView txtShortcutName;

        public Button btnShortcutUp;
        public Button btnShortcutAddFav;

//        public ImageView imgAppImg;
//        public TextView txtAppName;
    }

    public ImageLoader imageLoader; //用来下载图片的类，后面有介绍

    private final LayoutInflater mLayoutInflater;
    private final Random mRandom;
    private final ArrayList<Integer> mBackgroundColors;

    private static final SparseArray<Double> sPositionHeightRatios = new SparseArray<Double>();

    public ShortcutAdapter(final Context context, final int textViewResourceId) {
        super(context, textViewResourceId);
        mLayoutInflater = LayoutInflater.from(context);
        mRandom = new Random();
        mBackgroundColors = new ArrayList<>();
        mBackgroundColors.add(R.color.orange);
        mBackgroundColors.add(R.color.green);
        mBackgroundColors.add(R.color.blue);
        mBackgroundColors.add(R.color.yellow);
        mBackgroundColors.add(R.color.grey);
        imageLoader = new ImageLoader(context.getApplicationContext());
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        ViewHolder vh;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.shortcut_list_item, parent, false);
            vh = new ViewHolder();
            vh.txtShortcut = (TextView) convertView.findViewById(R.id.shortcut);
            vh.txtShortcutName = (TextView) convertView.findViewById(R.id.shortcut_name);
            vh.txtShortcutDes = (TextView) convertView.findViewById(R.id.shortcut_des);

            vh.btnShortcutAddFav = (Button) convertView.findViewById(R.id.shortcut_add_fav);
            vh.btnShortcutUp = (Button) convertView.findViewById(R.id.shortcut_up);
//            vh.imgAppImg = (ImageView) convertView.findViewById(R.id.app_img);
//            vh.txtAppName = (TextView) convertView.findViewById(R.id.app_name);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        double positionHeight = getPositionRatio(position);


        JSONObject obj = getItem(position);
        String shortcut = Utils.getStringFromJson(obj, "shortcut");
        String name = Utils.getStringFromJson(obj, "name");
        String des = Utils.getStringFromJson(obj, "des");
        String up = Utils.getStringFromJson(obj, "up");
        String skin = Utils.getStringFromJson(obj, "skin");

        String upTxt = String.format("%s%s", up, "赞");

        int skinIdx = Integer.parseInt(skin);
        int backgroundIndex = skinIdx >= mBackgroundColors.size() ?
                skinIdx % mBackgroundColors.size() : skinIdx;

        convertView.setBackgroundResource(mBackgroundColors.get(backgroundIndex));

        Log.d(TAG, "getView skinIdx:" + skinIdx + " h:" + positionHeight);


        vh.txtShortcut.setText(shortcut);
        vh.btnShortcutUp.setText(upTxt);
        vh.txtShortcutDes.setText(des);
        vh.txtShortcutName.setText(name);
        vh.txtShortcutDes.setText(des);

//        vh.txtAppName.setText(appName);
        Log.i(TAG, obj.toString());

        vh.btnShortcutAddFav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                Toast.makeText(getContext(), "Button Clicked Position " +
                        position, Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }

    private double getPositionRatio(final int position) {
        double ratio = sPositionHeightRatios.get(position, 0.0);
        // if not yet done generate and stash the columns height
        // in our real world scenario this will be determined by
        // some match based on the known height and width of the image
        // and maybe a helpful way to get the column height!
        if (ratio == 0) {
            ratio = getRandomHeightRatio();
            sPositionHeightRatios.append(position, ratio);
            Log.d(TAG, "getPositionRatio:" + position + " ratio:" + ratio);
        }
        return ratio;
    }

    private double getRandomHeightRatio() {
        return (mRandom.nextDouble() / 2.0) + 1.0; // height will be 1.0 - 1.5 the width
    }
}